package com.platform.modules.wallet.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.platform.common.constant.AppConstants;
import com.platform.common.exception.BaseException;
import com.platform.common.shiro.ShiroUtils;
import com.platform.common.shiro.utils.Md5Utils;
import com.platform.modules.chat.domain.ChatUser;
import com.platform.modules.wallet.domain.Wallet;
import com.platform.modules.wallet.service.WalletService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Slf4j
@Component
public class TransferUtils {

    @Resource
    private WalletService walletService;

    public void verifySend(String content, Wallet wallet, ChatUser cu) {

        JSONObject json = JSONUtil.parseObj(content);

        String payPassword = json.getStr("payPassword");
        int amount = new BigDecimal(json.getStr("amount")).multiply(BigDecimal.valueOf(100)).intValue();
        payPassword = Md5Utils.credentials(payPassword, cu.getSalt());

        if (!StrUtil.equals(payPassword, wallet.getPayPassword())) {
            throw new BaseException("交易密码有误，请重试");
        }
        if (walletService.getBalanceNow(wallet.getUserId()) < amount) {
            throw new BaseException("失败：金额超出余额");
        }

        this.verifyAccount();
    }

    public void verifyAccount() {

        //校验资金安全
        Long userId = ShiroUtils.getUserId();
        verifyAccount(userId);
    }


    public void verifyAccount(Long userId) {

        if (AppConstants.SYSTEM_CLEAR_ACCOUNT == 1) {
            throw new BaseException("系统正在清算，暂停交易，请稍后再试！");
        }
        DateTime date = DateUtil.date();
        DateTime dateTime = DateUtil.beginOfDay(date);
        long beginTime = dateTime.getTime() / 1000l;
        long nowTime = date.getTime() / 1000l;
//        System.out.println(nowTime - beginTime);
        if (nowTime - beginTime < 120) {
            throw new BaseException("系统正在清算，暂停交易，请稍后再试！");
        }
//        List<WalletDailyBill> walletDailyBills = walletDailyBillhService.queryList(new WalletDailyBill().setUserId(userId));

//        for (WalletDailyBill walletDailyBill : walletDailyBills) {
//            if (!walletDailyBill.getKey().equals(walletDailyBill.getPrivateKey())){
//                log.error(StrFormatter.format("账单校验失败 id:{} userId:{} privateKey:{} key:{}",
//                        walletDailyBill.getId(),walletDailyBill.getUserId(),walletDailyBill.getPrivateKey(),walletDailyBill.getKey()));
//                throw new BaseException(StrFormatter.format("账单校验失败BillID:{}",walletDailyBill.getId()));
//            }
//        }
//
//        List<WalletAccount> walletAccounts = accountService.queryPage(1,100).getRecords();
//        for (WalletAccount walletAccount : walletAccounts) {
//            if (!walletAccount.getKey().equals(walletAccount.getPrivateKey())){
//                userService.deleteById(walletAccount.getUserId());
//                log.error(StrFormatter.format("账单校验失败 id:{} userId:{} privateKey:{} key:{}",
//                        walletAccount.getId(),walletAccount.getUserId(),walletAccount.getPrivateKey(),walletAccount.getKey()));
//                throw new BaseException(StrFormatter.format("账单校验失败AccountID:{}",walletAccount.getId()));
//            }
//        }
    }

}
